package Download;

import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import DAO.L_likeDAO;
import DAO.SongDOA;
import DAO.Song_ListDAO;
import each_class.Album;
import each_class.RanKingClass;
import each_class.SongClass;
import each_class.User;
import DAO.Singers_DAO;

public class CancelDelete {

	public static void disFile(String path, SongClass songClass) {
		File file1 = new File(path + "\\" + songClass.getSong_name() + songClass.getSong_id() + ".mp3");
		File file2 = new File(path + "\\" + songClass.getSong_name() + songClass.getSong_id() + ".jpg");
		File file3 = new File(path + "\\" + songClass.getSong_name() + songClass.getSong_id() + ".obj");
		if (file1.exists() && file1.isFile()) {
			file1.delete();
		}

		if (file2.exists() && file2.isFile()) {
			file2.delete();
		}

		if (file3.exists() && file3.isFile()) {
			file3.delete();
		}

	}

	// 取消喜欢
	public static void cancelLike(SongClass songClass, User us) throws Exception {
		L_likeDAO.dissongLike(us.getUser_id(), songClass.getSong_id());

		if (Song_ListDAO.judgeExist(songClass.getSong_id())) {
			SongDOA.disSong(songClass.getSong_id());
			Singers_DAO.disSingers(songClass.getSingerid());

		}
		disFile(songClass.getCachingpath(), songClass);
	}

	public static void cancelLike(RanKingClass rkc, User us) throws Exception {
		L_likeDAO.disListLike(us.getUser_id(), rkc.getId());
		Song_ListDAO.disSong_list(us.getUser_id(), rkc.getId());
		List<Map<String, Object>> lmap = Song_ListDAO.findSongAll(us.getUser_id(), rkc.getId());

		for (Map<String, Object> m : lmap) {
			for (Entry<String, Object> entry : m.entrySet()) {
				switch (entry.getKey()) {
				case "song_id":
					Song_ListDAO.disSong(us.getUser_id(), rkc.getId(), (String) entry.getValue());
					if (Song_ListDAO.judgeExist((String) entry.getValue())) {
						SongDOA.disSong((String) entry.getValue());
					}
					break;
				}
			}
		}

	}

	public static void cancelLike(Album album, User us) throws Exception {
		L_likeDAO.disalbum(us.getUser_id(), album.getAlbm_id());
		Song_ListDAO.disSong_list(us.getUser_id(), album.getAlbm_id());
		List<Map<String, Object>> lmap = Song_ListDAO.findSongAll(us.getUser_id(), album.getAlbm_id());

		for (Map<String, Object> m : lmap) {
			for (Entry<String, Object> entry : m.entrySet()) {
				switch (entry.getKey()) {
				case "song_id":
					Song_ListDAO.disSong(us.getUser_id(), album.getAlbm_id(), (String) entry.getValue());
					if (Song_ListDAO.judgeExist((String) entry.getValue())) {
						SongDOA.disSong((String) entry.getValue());
					}
					break;
				}
			}
		}

	}

	public static void disSong(SongClass songClass, User us) throws Exception {
		Song_ListDAO.disSong(us.getUser_id(), us.getUser_id(), songClass.getSong_id());

		if (Song_ListDAO.judgeExist(songClass.getSong_id())) {
			SongDOA.disSong(songClass.getSong_id());
			Singers_DAO.disSingers(songClass.getSingerid());
		}
		disFile(songClass.getDowpath(), songClass);
	}

}
